<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生成绩排名 - 教师端</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --info: #2196f3;
            --light: #f8f9fa;
            --dark: #343a40;
            --card-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fb;
            color: #333;
            min-height: 100vh;
        }

        /* 导航栏样式 */
        .navbar {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 0.8rem 1rem;
        }

        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
        }

        .navbar-brand i {
            margin-right: 10px;
            font-size: 1.8rem;
        }

        .nav-link {
            color: rgba(255, 255, 255, 0.85) !important;
            font-weight: 500;
            padding: 0.5rem 1rem !important;
            border-radius: 4px;
            margin: 0 3px;
            transition: var(--transition);
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.15);
            color: white !important;
        }

        /* 主内容区域 */
        .main-content {
            padding: 2rem 0;
        }

        .page-header {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            box-shadow: var(--card-shadow);
            margin-bottom: 2rem;
            position: relative;
            overflow: hidden;
        }

        .page-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 5px;
            height: 100%;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
        }

        .page-title {
            font-weight: 700;
            margin-bottom: 0.5rem;
            color: var(--dark);
        }

        .page-subtitle {
            color: #6c757d;
            margin-bottom: 0;
        }

        /* 排名卡片 */
        .ranking-card {
            background: white;
            border-radius: 12px;
            box-shadow: var(--card-shadow);
            margin-bottom: 2rem;
        }

        .ranking-header {
            background: white;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            padding: 1.25rem 1.5rem;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .ranking-header i {
            margin-right: 10px;
            font-size: 1.25rem;
            color: var(--primary);
        }

        .ranking-item {
            padding: 1.5rem;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            transition: var(--transition);
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .ranking-item:hover {
            background-color: rgba(67, 97, 238, 0.05);
        }

        .ranking-item:last-child {
            border-bottom: none;
        }

        .ranking-info {
            display: flex;
            align-items: center;
            flex-grow: 1;
        }

        .ranking-number {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: var(--primary);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 1.2rem;
            margin-right: 1.5rem;
            flex-shrink: 0;
        }

        .ranking-number.top1 {
            background: linear-gradient(135deg, #ffd700, #ffed4e);
            color: #333;
            box-shadow: 0 4px 15px rgba(255, 215, 0, 0.3);
        }

        .ranking-number.top2 {
            background: linear-gradient(135deg, #c0c0c0, #e0e0e0);
            color: #333;
            box-shadow: 0 4px 15px rgba(192, 192, 192, 0.3);
        }

        .ranking-number.top3 {
            background: linear-gradient(135deg, #cd7f32, #daa520);
            color: white;
            box-shadow: 0 4px 15px rgba(205, 127, 50, 0.3);
        }

        .student-info {
            flex-grow: 1;
        }

        .student-info h5 {
            margin-bottom: 0.25rem;
            font-weight: 600;
            color: var(--dark);
        }

        .student-info .student-id {
            color: #6c757d;
            font-size: 0.875rem;
            margin-bottom: 0.5rem;
        }

        .student-stats {
            display: flex;
            gap: 1.5rem;
            font-size: 0.875rem;
            color: #6c757d;
        }

        .student-stat {
            display: flex;
            align-items: center;
        }

        .student-stat i {
            margin-right: 0.5rem;
            color: var(--primary);
        }

        .ranking-score {
            text-align: right;
            flex-shrink: 0;
        }

        .ranking-score .score {
            font-size: 2rem;
            font-weight: 700;
            color: var(--primary);
            margin-bottom: 0.25rem;
        }

        .ranking-score .exam-count {
            font-size: 0.875rem;
            color: #6c757d;
        }

        /* 搜索框 */
        .search-box {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: var(--card-shadow);
            margin-bottom: 2rem;
        }

        .search-input {
            border: 2px solid #e9ecef;
            border-radius: 8px;
            padding: 0.75rem 1rem;
            font-size: 1rem;
            transition: var(--transition);
        }

        .search-input:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 0.2rem rgba(67, 97, 238, 0.25);
        }

        /* 加载动画 */
        .loading {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 200px;
        }

        .spinner-border {
            color: var(--primary);
        }

        /* 错误提示 */
        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1rem;
            border: 1px solid #f5c6cb;
        }

        /* 空状态 */
        .empty-state {
            text-align: center;
            padding: 3rem 1rem;
            color: #6c757d;
        }

        .empty-state i {
            font-size: 3rem;
            margin-bottom: 1rem;
            color: #dee2e6;
        }

        .footer {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 2rem 0;
            margin-top: auto;
        }
        .teacher .footer {
            background: linear-gradient(135deg, var(--teacher-accent), #3a0ca3);
        }
        .copyright {
            text-align: center;
            padding-top: 1.5rem;
            margin-top: 1.5rem;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            color: rgba(255, 255, 255, 0.7);
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <!-- 导航栏（与主页一致） -->
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container">
            <a class="navbar-brand" href="#">
                <i class="fas fa-chalkboard-teacher"></i>在线考试系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/home/manage"><i class="fas fa-home"></i> 主页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/exam/manage"><i class="fas fa-tasks"></i> 考试管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/teacher/question/manage"><i class="fas fa-book"></i> 题库管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/teacher/score/analysis"><i class="fas fa-chart-bar"></i> 成绩分析</a>
                    </li>
                </ul>
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/teacher/profile">
                            <i class="fas fa-user-circle"></i> 个人信息
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/logout">
                            <i class="fas fa-sign-out-alt"></i> 退出登录
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主内容 -->
    <div class="container main-content">
        <!-- 页面标题 -->
        <div class="page-header">
            <h1 class="page-title">
                <i class="fas fa-trophy"></i> 学生成绩排名
            </h1>
            <p class="page-subtitle">查看所有学生的成绩排名和统计信息</p>
        </div>

        <!-- 错误提示 -->
        <div id="errorMessage" class="error-message" style="display: none;"></div>

        <!-- 搜索框 -->
        <div class="search-box">
            <div class="row">
                <div class="col-md-6">
                    <input type="text" id="searchInput" class="form-control search-input" 
                           placeholder="搜索学生姓名或学号..." onkeyup="filterStudents()">
                </div>
                <div class="col-md-6">
                    <div class="d-flex justify-content-end">
                        <span class="text-muted mt-2">
                            <i class="fas fa-info-circle"></i>
                            共 <span id="totalStudents">0</span> 名学生
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <!-- 排名列表 -->
        <div class="ranking-card">
            <div class="ranking-header">
                <div>
                    <i class="fas fa-trophy"></i>
                    学生成绩排名
                </div>
                <div>
                    <span class="badge bg-primary" id="displayedCount">0</span>
                </div>
            </div>
            <div id="rankingList">
                <div class="loading">
                    <div class="spinner-border" role="status">
                        <span class="visually-hidden">加载中...</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚（与主页一致） -->
    <footer class="footer">
        <div class="container text-center py-4">
            <a href="https://gitee.com/sorrymaker04/onlineexam-workload" target="_blank"><i class="fab fa-github fa-2x"></i></a>
            <div class="copyright mt-2">
                &copy; 2025 版权所有 北城在线考试系统小组 12 Team
            </div>
        </div>
    </footer>

    <script src="/js/bootstrap.bundle.min.js"></script>
    <script>
        // 全局变量
        let allStudents = [];
        let filteredStudents = [];

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadStudentRanking();
        });

        // 加载学生排名数据
        function loadStudentRanking() {
            fetch('/teacher/score/api/student-ranking')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        allStudents = data.data;
                        filteredStudents = [...allStudents];
                        updateStudentRanking(data.data);
                        updateCounts();
                    } else {
                        showError('获取学生排名失败：' + data.message);
                    }
                })
                .catch(error => {
                    console.error('获取学生排名失败:', error);
                    showError('获取学生排名失败');
                });
        }

        // 更新学生排名
        function updateStudentRanking(students) {
            const container = document.getElementById('rankingList');
            
            if (!students || students.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <i class="fas fa-users"></i>
                        <h5>暂无排名数据</h5>
                        <p>还没有学生参加考试或完成考试</p>
                    </div>
                `;
                return;
            }

            const html = students.map((student, index) => {
                let rankClass = '';
                if (index === 0) rankClass = 'top1';
                else if (index === 1) rankClass = 'top2';
                else if (index === 2) rankClass = 'top3';

                return `
                    <div class="ranking-item">
                        <div class="ranking-info">
                            <div class="ranking-number ${rankClass}">${student.rank}</div>
                            <div class="student-info">
                                <h5>${student.studentName}</h5>
                                <div class="student-id">${student.studentId}</div>
                                <div class="student-stats">
                                    <div class="student-stat">
                                        <i class="fas fa-file-alt"></i>
                                        <span>${student.examCount} 次考试</span>
                                    </div>
                                    <div class="student-stat">
                                        <i class="fas fa-chart-line"></i>
                                        <span>平均分 ${student.averageScore.toFixed(1)}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="ranking-score">
                            <div class="score">${student.averageScore.toFixed(1)}</div>
                            <div class="exam-count">${student.examCount} 次考试</div>
                        </div>
                    </div>
                `;
            }).join('');

            container.innerHTML = html;
        }

        // 过滤学生
        function filterStudents() {
            const searchTerm = document.getElementById('searchInput').value.toLowerCase();
            
            if (!searchTerm) {
                filteredStudents = [...allStudents];
            } else {
                filteredStudents = allStudents.filter(student => 
                    student.studentName.toLowerCase().includes(searchTerm) ||
                    student.studentId.toLowerCase().includes(searchTerm)
                );
            }
            
            updateStudentRanking(filteredStudents);
            updateCounts();
        }

        // 更新计数
        function updateCounts() {
            document.getElementById('totalStudents').textContent = allStudents.length;
            document.getElementById('displayedCount').textContent = filteredStudents.length;
        }

        // 显示错误信息
        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
            
            setTimeout(() => {
                errorDiv.style.display = 'none';
            }, 5000);
        }
    </script>
</body>
</html> 